package com.ehome.poct.backend.serivce;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.ehome.core.page.Page;
import com.ehome.core.util.BeanConvertUtil;
import com.ehome.exception.BizException;
import com.ehome.join.annotation.JoinConfig;
import com.ehome.oss.config.OssClient;
import com.ehome.poct.backend.util.ByteSizeUtil;
import com.ehome.poct.base.api.constant.SyncStatus;
import com.ehome.poct.base.api.dto.log.TestDeviceSyncLogRecordDTO;
import com.ehome.poct.base.api.feign.device.TermClinicFeginClient;
import com.ehome.poct.base.api.feign.log.TestDeviceLogFeginClient;
import com.ehome.poct.base.api.qry.ErrorLogQry;
import com.ehome.poct.base.api.qry.LogSyncRecordQry;
import com.ehome.poct.base.api.vo.log.SyncLogRecordListVO;
import com.ehome.poct.base.api.vo.log.TestDeviceErrorLogVO;
import com.ehome.poct.base.api.vo.log.TestDeviceSyncLogRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
@Slf4j
public class LogSerivce {

    @Autowired
    TestDeviceLogFeginClient testDeviceLogFeginClient;
    @Autowired
    TermClinicFeginClient termClinicFeginClient;
    @Autowired
    OssClient ossClient;

    public TestDeviceSyncLogRecordVO syncLog(TestDeviceSyncLogRecordDTO dto) {
        //拼装对象
        dto.setSyncStatus(SyncStatus.SYNCING.ordinal());
        dto.setSyncTime(LocalDateTime.now());
       return testDeviceLogFeginClient.insertSyncLog(dto);
    }

    public TestDeviceSyncLogRecordVO batchSyncLog(List<TestDeviceSyncLogRecordDTO> list) {
        for (TestDeviceSyncLogRecordDTO dto : list) {
            //拼装对象
            dto.setSyncStatus(SyncStatus.SYNCING.ordinal());
            dto.setSyncTime(LocalDateTime.now());
        }
       return testDeviceLogFeginClient.batchInsertSyncLog(list);
    }

    public void againSyncLog(TestDeviceSyncLogRecordDTO dto) {
        dto.setSyncStatus(SyncStatus.SYNCING.ordinal());
        dto.setSyncTime(LocalDateTime.now());
        log.info("againSyncLog sno:{}",dto.getSno());
        testDeviceLogFeginClient.updateSyncLog(dto.getSyncLogRecordId(),dto);
    }

    @JoinConfig(value = TermClinicFeginClient.class,methodName = "list")
    public Page<TestDeviceErrorLogVO> getErrorLog(ErrorLogQry qry) {
        Page<TestDeviceErrorLogVO> page = testDeviceLogFeginClient.getErrorLog(qry);
        for (TestDeviceErrorLogVO vo : page.getList()) {
            String url = ossClient.getUrl(vo.getBucketName(), vo.getOssPath(), null);
            vo.setUrl(url);
            if(vo.getLogSize() != null){
                String size = ByteSizeUtil.getSize(vo.getLogSize());
                vo.setLogSizeStr(size);
            }
        }
        return page;
    }

    @JoinConfig(value = TermClinicFeginClient.class,methodName = "list")
    public List<TestDeviceSyncLogRecordVO> getLogSyncRecord(LogSyncRecordQry qry) {
        List<TestDeviceSyncLogRecordVO> logSyncRecord = testDeviceLogFeginClient.getLogSyncRecord(qry);
        for (TestDeviceSyncLogRecordVO vo : logSyncRecord) {
            if(vo.getLogSize() != null){
                String size = ByteSizeUtil.getSize(vo.getLogSize());
                vo.setLogSizeStr(size);
            }
        }
        return logSyncRecord;
    }

    public TestDeviceSyncLogRecordVO getLogSyncRecordById(Long id) {
        TestDeviceSyncLogRecordVO logSyncRecordById = testDeviceLogFeginClient.getLogSyncRecordById(id);
        Integer syncStatus = logSyncRecordById.getSyncStatus();
        if(syncStatus.equals(SyncStatus.SYNCING.ordinal())){
            LocalDateTime syncTime = logSyncRecordById.getSyncTime();
            long between = LocalDateTimeUtil.between(syncTime, LocalDateTime.now(), ChronoUnit.MINUTES);
            if(between >= 10){
                logSyncRecordById.setSyncStatus(SyncStatus.SYNC_FAILED.ordinal());
                TestDeviceSyncLogRecordDTO dto = BeanConvertUtil.convertBean(logSyncRecordById, TestDeviceSyncLogRecordDTO.class);
                testDeviceLogFeginClient.updateSyncLog(logSyncRecordById.getSyncLogRecordId(),dto);
                log.info("getLogSyncRecordById sno:{}",dto.getSno());
                throw new BizException("日志同步超时");
            }
        }
        return logSyncRecordById;
    }

    public SyncLogRecordListVO getLogList(String sno) {
        SyncLogRecordListVO syncLogRecord = testDeviceLogFeginClient.getLogList(sno);
        if (syncLogRecord == null || CollUtil.isEmpty(syncLogRecord.getBatchPath())) {
            throw new BizException("获取日志清单列表为空，原因：设备未开机或者网络超时，请重新再试。");
        }
        for (SyncLogRecordListVO.BatchPath batchPath : syncLogRecord.getBatchPath()) {
            if(batchPath.getLogSize() != null){
                batchPath.setLogSizeStr(ByteSizeUtil.getSize(batchPath.getLogSize()));
            }
        }
        return syncLogRecord;
    }
}
